#include "amendwifi.h"
#include "ui_amendwifi.h"
#include <QStringListModel>
#include <QGraphicsDropShadowEffect>
#include <QHttpPart>
#include <QNetworkAccessManager>
#include <QJsonDocument>
#include <QJsonParseError>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include "md5rc4.h"
#include "messagepops.h"
#include <QListView>
#include <QStyledItemDelegate>
#include "operationdocument.h"
#include "function/function.h"

extern QByteArray token;//http返回id码

amendWiFi::amendWiFi(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::amendWiFi)
{
    ui->setupUi(this);

    this->setAttribute(Qt::WA_TranslucentBackground);//设置窗口背景透明
    this->setWindowFlags(Qt::FramelessWindowHint|Qt::Window);   //设置无边框窗口
//    this->setWindowFlags(Qt::Window);//将子窗口设置为非顶级窗口

    QWidget *parentWidget=this->parentWidget();
    QPoint globalPos = parentWidget->mapToGlobal(QPoint(0,0));//父窗口绝对坐标
    int x = globalPos.x() + (parentWidget->width() - this->width()) / 2 - 150;//x坐标
    int y = globalPos.y() + (parentWidget->height() - this->height()) / 2;//y坐标
    this->move(x, y);//窗口移动

    //设置具体阴影
    QGraphicsDropShadowEffect *shadow_effect = new QGraphicsDropShadowEffect(this);
    shadow_effect->setOffset(0, 0);

    //阴影颜色
    shadow_effect->setColor(QColor(38, 78, 119, 127));
    //阴影半径
    shadow_effect->setBlurRadius(10);
    ui->label->setGraphicsEffect(shadow_effect);

    ui->label_7->hide();//隐藏账号错误提示
    ui->lineEdit_2->setEchoMode(QLineEdit::Password);//隐藏密码

    QFont font;
    font.setLetterSpacing(QFont::PercentageSpacing,120);//120字间距，默认100
    ui->label_2->setFont(font);
    ui->label_3->setFont(font);
    ui->label_4->setFont(font);
    ui->label_5->setFont(font);
    ui->label_6->setFont(font);
    ui->label_7->setFont(font);
    ui->lineEdit->setFont(font);
    ui->lineEdit_2->setFont(font);

    mps=new MessagePops(this);//初始化提示窗口对象
    mps->hide();
    mps->setGeometry(55,90,270,220);
    connect(mps,SIGNAL(sendMessage(QString)),this,SLOT(receMessage(QString)));

    mps2=new MessagePops(this);//初始化提示窗口对象(不关联信号与槽)
    mps2->hide();
    mps2->setGeometry(55,90,270,220);

    //初始化定时器对象
    deletetime=new QTimer(this);
    //设置定时时间
    deletetime->setInterval(100);  //0.1秒
    //关联timeout()信号与自定义的槽函数
    connect(deletetime,SIGNAL(timeout()),this,SLOT(deletetimer()));

    setcomboBox(ui->comboBox);
    setcomboBox(ui->comboBox_2);

    //禁用下拉框
//    ui->comboBox->setEnabled(false);
//    ui->comboBox_2->setEnabled(false);
//    ui->lineEdit->setEnabled(false);//禁用WiFi账号输入框
    ui->comboBox->hide();
    ui->comboBox_2->hide();
    ui->lineEdit->hide();//禁用WiFi账号输入框

    ifType();

    setpush();
}

amendWiFi::~amendWiFi()
{
    delete ui;
}

void amendWiFi::setInio(QStringList data, QJsonObject userinfo)
{
    accountNumbers_ = data;
    userinfo_ = userinfo;

    id = userinfo_.value("id").toInt();
    name = userinfo_.value("name").toString();
    oldname = name;
    password = userinfo_.value("password").toString();
    QByteArray st_buf=QByteArray::fromHex(password.toUtf8());
    char* sTemp4 = st_buf.data();
    unsigned long len = st_buf.size();
    Md5Rc4 *mr=new Md5Rc4();//初始化加解密对象
    mr->rc4EncryptionDecode(sTemp4, len);
    delete mr;
    QByteArray array=QByteArray(sTemp4,len);
    password=array;

    int typenum = userinfo_.value("type").toInt();
    if(typenum == 1)
    {
        type = "公共";
    }
    else
    {
        type = "药柜";
    }
    cabinet = QString::number(userinfo_.value("cabinet_number").toInt());

    ui->comboBox->setCurrentIndex(typenum-1);
    if(cabinet.toInt() <= 0)
    {
        ui->comboBox_2->setCurrentIndex(0);
    }
    else
    {
        ui->comboBox_2->setCurrentIndex(cabinet.toInt()-1);
    }

    ui->label_8->setText(type);
    ui->label_9->setText(cabinet);
    ui->label_10->setText(name);


    ui->lineEdit->setText(name);
    ui->lineEdit_2->setText(password);

    ifType();
    setpush();
}

void amendWiFi::receMessage(QString data)
{
    if(data == "1")
    {
        //上传成功点击了确定
        emit sendAmendWifi("1");
        deletetime->start();//开启关闭窗口定时器
    }
    else if(data == "2")
    {
        //点击了否
    }
    else if(data == "3")
    {
        //点击了是
        sendWIFIinfo();//发送WIFI信息
    }
}

//点击了确定按钮
void amendWiFi::on_pushButton_clicked()
{
    //获取QComboBox列表项的当前项的文字
    type=comboBoxText(ui->comboBox);//类型 [1:公共2:药柜]
    cabinet=comboBoxText(ui->comboBox_2);//药柜编号

    name=ui->lineEdit->text();//wifi名
    password=ui->lineEdit_2->text();//加密后的密码

    if(name.isEmpty())
    {
        qDebug()<<__LINE__<<"不能为空账号";
        return;
    }


    if(password.isEmpty())
    {
        mps->setTexts("当前WIFI设置的密码为空，@#是否继续创建该WIFI信息？");
        mps->show();
        return;
    }
    else
    {
        Md5Rc4 *mr=new Md5Rc4();//初始化加解密对象
        int strlen=password.size();
        QByteArray ba = password.toUtf8();
        char *ch=ba.data();
        mr->rc4EncryptionDecode(ch, strlen);
        delete mr;

        QByteArray array=QByteArray(ch,strlen);
        password=array.toHex();

        qDebug()<<__LINE__<<"password="<<password;
    }

    sendWIFIinfo();//发送WIFI信息
}

//显示隐藏密码
void amendWiFi::on_pushButton_2_clicked()
{
    //设置密码隐藏
    if(Password_conceal)
    {
        Password_conceal=false;
        ui->lineEdit_2->setEchoMode(QLineEdit::Password);//隐藏密码
        ui->pushButton_2->setStyleSheet("border-image: url(:/new/prefix1/resource/15.png);");
    }
    else
    {
        Password_conceal=true;
        ui->lineEdit_2->setEchoMode(QLineEdit::Normal);//显示密码
        ui->pushButton_2->setStyleSheet("border-image: url(:/new/prefix1/resource/16.png);");
    }
}

//点击关闭按钮
void amendWiFi::on_pushButton_5_clicked()
{
    emit sendAmendWifi("2");
    deletetime->start();//开启关闭窗口定时器
}

//点击了类型复选框
void amendWiFi::on_comboBox_activated(const QString &)
{
    ifType();
}

//判断WiFi类型显示界面
void amendWiFi::ifType()
{
    //获取QComboBox列表项的当前项的文字
    QString type = comboBoxText(ui->comboBox);//类型

    if(type == "公共")
    {
        ui->label_4->hide();
        ui->label_5->move(30,162);
        ui->label_6->move(30,220);
        ui->label_7->move(100,260);
        ui->pushButton_2->move(275,230);

        ui->lineEdit->move(100,162);
        ui->lineEdit_2->move(100,220);
        ui->label_10->move(100,162);

        ui->comboBox_2->hide();
        ui->label_9->hide();
    }
    else if(type == "药柜")
    {
        ui->label_4->show();
//        ui->comboBox_2->show();

        ui->label_4->move(30,162);
        ui->comboBox_2->move(100,162);
        ui->label_5->move(30,220);
        ui->lineEdit->move(100,220);
        ui->label_6->move(30,280);
        ui->lineEdit_2->move(100,280);
        ui->label_7->move(100,320);
        ui->pushButton_2->move(275,290);

        ui->label_9->show();
        ui->label_9->move(100, 162);
        ui->label_10->move(100, 220);
    }
}

//解析获取comboBox选中文本
QString amendWiFi::comboBoxText(QComboBox *comboBox)
{
    QString text = comboBox->currentText();
    text = text.replace(" ","");

    return text;
}

//发送WIFI信息
void amendWiFi::sendWIFIinfo()
{
    //发送WiFi信息
    QNetworkRequest request;
    QNetworkAccessManager* naManager = new QNetworkAccessManager(this);
    connect(naManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));

    request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json"));
    request.setUrl(QUrl::fromUserInput(URLName_2("update-wifi-password/")));
    request.setRawHeader("token",token);

    QJsonObject object;
    object.insert("id",id);//id
    object.insert("name",name);//wifi名
    object.insert("password",password);//加密后的密码
    object.insert("type",type);//类型 [1:公共2:药柜]
    object.insert("cabinet",cabinet);//药柜编号

    QByteArray byte_array = QJsonDocument(object).toJson();
    qDebug()<<__LINE__<< "user  info " << object;

    naManager->post(request,byte_array);//发起post请求

    mps->setText0("正在上传信息");
    mps->show();
}

//设置QComboBox样式
void amendWiFi::setcomboBox(QComboBox *comboBox)
{
    comboBox->setView(new QListView());
    QStyledItemDelegate* itemDelegate = new QStyledItemDelegate();
    comboBox->setItemDelegate(itemDelegate);

    //设置标题字间距,外阴影
    QFont font;
    font.setFamily(QStringLiteral("Microsoft YaHei"));//设置字体
    font.setPixelSize(15);//字体大小
    font.setLetterSpacing(QFont::PercentageSpacing,120);//120字间距，默认100
    comboBox->setFont(font);

    QAbstractItemView* view = comboBox->view();
    view->setFont(font);//设置标题字间距
    QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect;
    effect->setBlurRadius(10);//阴影半径
//    effect->setColor(Qt::black);
    effect->setColor(QColor(38, 78, 119, 127));//阴影颜色
    effect->setOffset(0, 0);
    view->setGraphicsEffect(effect);
    view->update();

    static_cast<QDialog*>(view->parent())->setStyleSheet("QDialog{border:none;background-color: rgb(255, 255, 255);}");

    QDialog* container = static_cast<QDialog*>(view->parent());
//    container->setAttribute(Qt::WA_TranslucentBackground);//设置窗口透明
    container->setWindowFlag(Qt::FramelessWindowHint);
    container->setWindowFlag(Qt::NoDropShadowWindowHint);
}

//设置是否禁用确定按钮 样式
void amendWiFi::setpush()
{
    if(ui->lineEdit->text().isEmpty() || !ifrepetition)
    {
        //当输入框没有内容时禁止按钮
        ui->pushButton->setEnabled(false);
        ui->pushButton->setStyleSheet("background: #CCCCCC;border-bottom-left-radius :16px;border-bottom-right-radius :16px;font-size: 18px;font-family: Microsoft YaHei;color: #FBFBFB;");
    }
    else
    {
        ui->pushButton->setEnabled(true);
        ui->pushButton->setStyleSheet("background: #1B89CC;border-bottom-left-radius :16px;border-bottom-right-radius :16px;font-size: 18px;font-family: Microsoft YaHei;color: #FBFBFB;");
    }
}



void amendWiFi::on_lineEdit_textChanged(const QString &arg1)
{
    if(arg1.isEmpty())
    {
        OperationDocument::setqss(ui->lineEdit,":/new/prefix1/resource/myStyleSheet2.qss");

//        ui->lineEdit->setStyleSheet("border: 1px solid #666666;border-radius: 8px;padding-left:8px;padding-bottom:2px;font-size: 16px;font-family: Microsoft YaHei;color: #999999;");
    }
    else
    {
        OperationDocument::setqss(ui->lineEdit,":/new/prefix1/resource/myStyleSheet.qss");

//        ui->lineEdit->setStyleSheet("border: 1px solid #666666;border-radius: 8px;padding-left:8px;padding-bottom:2px;font-size: 16px;font-family: Microsoft YaHei;color: #333333;");
    }

    for(int i=0;i<accountNumbers_.size();i++)
    {
        if(accountNumbers_.at(i) == arg1 && oldname != arg1)
        {
            ui->label_7->show();//账号已经存在提示
            ifrepetition = false;
            setpush();
            return;
        }
    }
    ui->label_7->hide();
    ifrepetition = true;
    setpush();
}

void amendWiFi::on_lineEdit_2_textChanged(const QString &arg1)
{
    if(arg1.isEmpty())
    {
        OperationDocument::setqss(ui->lineEdit_2,":/new/prefix1/resource/myStyleSheet2.qss");

//        ui->lineEdit_2->setStyleSheet("border: 1px solid #666666;border-radius: 8px;padding-left:8px;padding-bottom:2px;font-size: 16px;font-family: Microsoft YaHei;color: #999999;");
    }
    else
    {
        OperationDocument::setqss(ui->lineEdit_2,":/new/prefix1/resource/myStyleSheet.qss");

//        ui->lineEdit_2->setStyleSheet("border: 1px solid #666666;border-radius: 8px;padding-left:8px;padding-bottom:2px;font-size: 16px;font-family: Microsoft YaHei;color: #333333;");
    }
}

//发送WiFi信息接收槽函数
void amendWiFi::requestFinished(QNetworkReply *reply)
{
    QByteArray bytes = reply->readAll();

    qDebug() <<__LINE__<< QString(bytes) ;

    if(bytes.contains("ok"))
    {
        mps->setText("上传成功");
        mps->show();
    }
    else
    {
        mps->setText("上传失败");
        mps->show();
    }

}

void amendWiFi::deletetimer()
{
    deletetime->stop();//关闭定时器

    delete this;
}


